package j02hanshu;

import java.util.Random;
import java.util.function.*;

public class J1 {
    public static void main(String[] args) {
        Consumer<String> c1 = new Consumer<String>() {
            @Override
            public void accept(String s) {
                System.out.println("s = " + s);
            }
        };
        c1.accept("2000");

        Consumer<Integer> c2 = num-> System.out.println(num);
        c2.accept(200);
        System.out.println("====");
        Supplier<Integer> getNum10_20 = ()->{
            Random r = new Random();
            return r.nextInt(20-10)+1;
        };
        System.out.println(getNum10_20.get());
        System.out.println(getNum10_20.get());
        System.out.println(getNum10_20.get());
        System.out.println(getNum10_20.get());

        System.out.println("=====");
        Function<Integer,String> function = num->{
            //不兼容的类型。实际为 boolean'，需要
            // 'char、byte、short、int、String 或枚举'
            // Character、Byte、Short、Integer、
            switch (num){
                case 1:
                    return "可用";
                case 2:
                    return "外借";
                case 3:
                    return "损坏";
                default:
                    return "未知";
            }
        };
        System.out.println(function.apply(1));
        System.out.println(function.apply(2));
        System.out.println(function.apply(100));
        System.out.println(">>>>>>>>>");
        Predicate<Integer> p1 = score->score<60?true:false;
        System.out.println(p1.test(23));//true
        System.out.println(p1.test(60));//false

        System.out.println("<<<<<<<<<<<");
        UnaryOperator<Integer> u1 = num->num*2;
        System.out.println(u1.apply(2));//4
        System.out.println(u1.apply(10));//20
    }
}
class Cat{
    String name;
    int age;

    public Cat(String name, int age) {
        this.name = name;
        this.age = age;
    }

    @Override
    public String toString() {
        return "Cat{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }
}
class BiFunctionTest{
    public static void main(String[] args) {
        BiFunction<String,Integer,Cat> b1 = (name,age)->{
            return new Cat(name,age);
        };
        System.out.println(b1.apply("加菲猫",2));
    }
}